<template>
  <div class="wrap">
    <p class="p1">品类管理</p>
    <el-button class="btn" type="primary" icon="el-icon-plus" @click="add"
      >添加品类</el-button
    >

    <div class="body">
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="id" label="品类ID" width="150">
        </el-table-column>
        <el-table-column prop="name" label="商品名称"> </el-table-column>
        <el-table-column label="操作" width="300">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >修改名称</el-button
            >
            <el-button type="text" size="small" @click="handle(scope.row)" v-if="flg == true">查看其子品类</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      width="30%"
      :modal="flag"
    >
      <input type="text" name="" id="" class="input" v-model="title">
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogin">取 消</el-button>
        <el-button type="primary" @click="dialog"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getcategory, setcategory } from "@/utils/request.js";
export default {
  name: "Cart",
  data() {
    return {
      tableData: [],
      categoryId: "",
      categoryName: "",
      dialogVisible: false,
      flag:false,
      flg:true,
      title:""
    };
  },
  created() {
    this.getcate();
  },
  methods: {
    // 获取数据
    getcate() {
      getcategory(0).then((res) => {
        // console.log(res);
        this.tableData = res.data;
      });
    },
    // 添加品类
    add() {
      this.$router.push('/cartadd')
    },
    handleClick(row) {
      console.log(row);
      this.categoryId = row.id
      this.title = row.name
      this.dialogVisible = true
    },
    // 修改品类
    dialog(){
      this.categoryName = this.title
      this.title = ""
      setcategory({categoryId:this.categoryId,categoryName:this.categoryName}).then((res)=>{
        console.log(res);
        this.dialogVisible = false
        this.getcate();
      })
    },
    // 取消
    dialogin(){
      this.dialogVisible = false
    },
    // 查看子品类
    handle(row){
      getcategory(row.id).then((res) => {
        // console.log(res);
        this.tableData = res.data;
        this.flg = false
      });
    }
  },
  components: {},
};
</script>

<style lang="scss" scoped>
.wrap {
  position: relative;
  .p1 {
    text-align: left;
    font-size: 40px;
    margin: 10px 0 20px 0;
  }
  .btn {
    position: absolute;
    right: 20px;
    top: 0px;
  }
  .input{
    border: 2px solid blue;
    width: 80%;
    height: 40px;
  }
}
</style>
